//index.js
//获取应用实例
const app = getApp()
import Share from "../../lib/js/share.js";
var QQMapWX = require('../../lib/js/qqmap-wx-jssdk.js');
var qqmapsdk;
Page({
  data: {
    height: app.globalData.height,
    titleBarHeight: app.globalData.titleBarHeight,
    // 组件所需的参数
    nvabarData: {
      showCapsule: 0, //是否显示左上角图标   1表示显示    0表示不显示
      title: '首页', //导航栏 中间的标题
    },
    navHeight: app.globalData.navHeight,
    // 此页面 页面内容距最顶部的距离
    videoUrl: "",
    addr: "无法定位地址",
    isShow: false,
    costomerPhone: "",
    indexShow:false,
    token: "",
    phone: "",
    vpUrl: "",
    videoPic:"../../img/videos.png",
    isInit: false,
  },
  // 获取滚动条当前位置
  onPageScroll: function (e) {
    
  },
  //选择获取地理位置
  getAddress:function(){
     var that=this;
     app.getPermission(that);    //传入that值可以在app.js页面直接设置内容    
  }, 
  videoErrorCallback(e){
    console.log('视频错误信息' + e.detail.errMsg)
  },
  routePlan(){
    wx.openLocation({
      latitude:31.342085,
      longitude:120.943232,
      scale: 18,
      name: '江苏鸿鑫智能制造有限公司',
      address: '江苏鸿鑫智能制造有限公司'
    })
    return
    if (!this.data.token) {
      wx.navigateTo({
        url: '/packageD/pages/authorize/authorize'
      })
      return
    } 
    this.setData({
      indexShow: false,
      isShow: false,
    })
    let key="WCUBZ-YYHLW-TDWR3-YYUSM-LV4VT-S4BFN"
    let referer = "鸿鑫智造-小程序线路导航"
    let endPoint = JSON.stringify({
        name: "江苏鸿鑫智能制造有限公司",
        latitude:31.342085,
        longitude:120.943232
    })
    wx.navigateTo({
      url: 'plugin://routePlan/index?key=' + key +'&referer=' + referer + '&endPoint=' + endPoint
    })
  },
 
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  // /**
  // * 用户点击右上角分享
  // */
  // onShareAppMessage: function () {
  //   let that = this;
  //   return {
  //     title: that.data.shareInfo.title,
  //     path: '/pages/index/index?share=true&spread_code=' + that.data.spread_code,
  //     imageUrl: that.data.shareInfo.icon,
  //     success: function (res) {
  //       wx.showToast({
  //         title: "已转发",
  //         icon: 'none',
  //         duration: 2000
  //       })
  //     }
  //   }
  // },
  
  onPullDownRefresh() {
    let that = this;
    
    wx.stopPullDownRefresh(); //为停止当前页面下拉刷新

  },

  onLoad: function (options) {
    let that = this;
    qqmapsdk = new QQMapWX({
        key: 'WCUBZ-YYHLW-TDWR3-YYUSM-LV4VT-S4BFN'
    });
  
    that.getIndexInfo()
    
  },

  onShow: function () {
    let that = this;
    wx.showTabBar()
    let token = wx.getStorageSync('token') ? wx.getStorageSync('token') : ""
    that.setData({
      token: token
    })
   
  },
  // onPageScroll(e){
  //   let that = this;
  //   var scrollTop = parseInt(e.scrollTop); //滚动条距离顶部高度
  //   //判断'滚动条'滚动的距离 和 '元素在初始时'距顶部的距离进行判断
  //   var is_result = scrollTop >= that.data.top-that.data.navHeight-1 ? true : false;
  //   //为了防止不停的setData, 这儿做了一个等式判断。 只有处于吸顶的临界值才会不相等
  //   if (that.data.is_fixed === is_result) {
  //     return false;
  //   }
  //   that.setData({
  //     is_fixed: is_result
  //   });
  // },
  onReachBottom() {
      let that = this;
      that.getList();
  },
  toCollect(){
    if (!this.data.token) {
      wx.navigateTo({
        url: '/packageD/pages/authorize/authorize'
      })
      return
    } 
    this.setData({
      indexShow: false,
      isShow: false,
    })
    wx.navigateTo({
      url: '/packageD/pages/collect/collect'
    })
  },
  toArt(){
    if (!this.data.token) {
      wx.navigateTo({
        url: '/packageD/pages/authorize/authorize'
      })
      return
    } 
    wx.navigateTo({
      url: '/packageB/pages/artList/artList'
    })
  },
  toCraf(){
    if (!this.data.token) {
      wx.navigateTo({
        url: '/packageD/pages/authorize/authorize'
      })
      return
    } 
    wx.navigateTo({
      url: '/packageB/pages/crafList/crafList'
    })
  },
  toLive(){
    if (!this.data.token) {
      wx.navigateTo({
        url: '/packageD/pages/authorize/authorize'
      })
      return
    } 
    wx.navigateTo({
      url: '/packageB/pages/live/live?vpUrl=' + this.data.vpUrl
    })
  },
  getShow(){
    this.setData({
      isShow: true,
    })
  },
  closeShow(){
    this.setData({
      isShow: false,
    })
  },
  makeCall(e){
    // 调用微信的拨打电话API 安卓直接跳转到手机拨号页面  苹果要用户点击同意才跳转
    wx.makePhoneCall({
        phoneNumber: e.currentTarget.dataset.phone,
    })
 },
 cancelShow(){
    this.setData({
        indexShow: false,
      })
    },
    getIndexShow (e){
      let that = this
      let platform = wx.getDeviceInfo().system //判断使用设备
      
      if (platform.includes('iOS') || platform.includes('IOS') || platform.includes('ios')) {
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.phone,
        })
      } else {
        that.setData({
          indexShow: true,
          isShow: false,
        })
      }
      
    },
    getIndexInfo(){
      let that = this
      app.fly.get('/ma/home/init').then(function (res) {

        if (res.code === 0) {
          // wx.setStorageSync("token", "1")
            wx.setStorageSync("phone", res.data.telephone)
             that.setData({
              videoUrl: res.data.videoPreviewUrl,
              phone:res.data.telephone,
              vpUrl:res.data.videoUrl,
              isInit: true,
             })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 2000
          });
        }
      })
    },  
})
